#pragma once
#include "include/cef_app.h"
#include <list>
#include "sqlite3.h"

class PageHandler : public CefClient, public CefLifeSpanHandler, public CefJSDialogHandler, 
	public CefContextMenuHandler, public CefDragHandler
{
public:
	PageHandler() = default;
	// LifeSpan
	virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() override
	{
		return this;
	}

	virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) override;

	virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) override;

	bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
		CefRefPtr<CefFrame> frame,
		CefProcessId source_process,
		CefRefPtr<CefProcessMessage> message) override;

	// JsDialog
	CefRefPtr<CefJSDialogHandler> GetJSDialogHandler() override
	{
		return this;
	}

	bool OnJSDialog(CefRefPtr<CefBrowser> browser, const CefString& origin_url,
		JSDialogType dialog_type, 
		const CefString& message_text, 
		const CefString& default_prompt_text,
		CefRefPtr<CefJSDialogCallback> callback, bool& suppress_message) override;

	bool OnBeforeUnloadDialog(CefRefPtr<CefBrowser> browser,
		const CefString& message_text,
		bool is_reload, CefRefPtr<CefJSDialogCallback> callback) override;

	// contextmenu
	CefRefPtr <CefContextMenuHandler> GetContextMenuHandler() override
	{
		return this;
	}

	void OnBeforeContextMenu(CefRefPtr<CefBrowser> browser,
		CefRefPtr<CefFrame> frame,
		CefRefPtr<CefContextMenuParams> params,
		CefRefPtr<CefMenuModel> model) override;

	bool OnContextMenuCommand(CefRefPtr<CefBrowser> browser,
		CefRefPtr<CefFrame> frame,
		CefRefPtr<CefContextMenuParams> params,
		int command_id,
		EventFlags event_flags) override;
	
	//DragHandler
	CefRefPtr<CefDragHandler> GetDragHandler() override
	{
		return this;
	}

	void OnDraggableRegionsChanged(
		CefRefPtr<CefBrowser> browser,
		CefRefPtr<CefFrame> frame,
		const std::vector<CefDraggableRegion>& regions) override;

	/*void ReadFileByBlocks(const std::string& filePath,
		const std::string& msgName,
		CefRefPtr<CefFrame> frame);*/

private:
	IMPLEMENT_REFCOUNTING(PageHandler);
	std::list<CefRefPtr<CefBrowser>> m_browsers;
};

